<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container" @mouseleave="isShowIndex = -1">
      <div @mouseenter="enter" @mouseleave="leave">
        <h2 class="all">全部商品分类</h2>
        <!-- 2.根据当前的路径来判断当前是否显示隐藏 -->
        <transition name="navAnimation">
          <div class="sort" v-show="isShow">
            <div class="all-sort-list2">
              <div
                class="item"
                v-for="(cate, index) in categoryList"
                :key="cate.categoryId"
                :class="isShowIndex === index ? 'active' : ''"
                @mouseenter="fore(index)"
                @click="goSearch"
              >
                <h3>
                  <a
                    :data-category1Id="cate.categoryId"
                    :data-categoryName="cate.categoryName"
                    >{{ cate.categoryName }}</a
                  >
                </h3>
                <div class="item-list clearfix">
                  <div class="subitem">
                    <dl
                      class="fore"
                      v-for="child in cate.categoryChild"
                      :key="child.categoryId"
                    >
                      <dt>
                        <a
                          :data-category2Id="child.categoryId"
                          :data-categoryName="child.categoryName"
                          >{{ child.categoryName }}</a
                        >
                      </dt>
                      <dd>
                        <em
                          v-for="item in child.categoryChild"
                          :key="item.categoryId"
                        >
                          <a
                            :data-category3Id="item.categoryId"
                            :data-categoryName="item.categoryName"
                            >{{ item.categoryName }}</a
                          >
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
import _ from "lodash";
import { mapState } from "vuex";
export default {
  name: "NavType",
  data() {
    return {
      // 显示样式的下标
      isShowIndex: -1,
      isShow: this.$route.meta.isNav,
    };
  },
  mounted() {
    // 节流函数 会将传过来的index 传给onmouseEnter函数让他执行
    this.fore = _.throttle(this.onmouseEnter, 30, { trailing: false });
  },
  methods: {
    onmouseEnter(index) {
      this.isShowIndex = index;
    },
    // 点击搜索事件 事件代理 通过e.target判断当前元素节点的类型
    goSearch(e) {
      // 如果点击的元素是A元素则执行
      if (e.target.nodeName === "A") {
        const query = {};
        const params = this.$route.params;
        // 获取自定义属性上的值 因为在html上都是小写 所以小写获取
        const { category1id, category2id, category3id, categoryname } =
          e.target.dataset;
        query.categoryname = categoryname;
        if (category1id) {
          query.category1id = category1id;
        }
        if (category2id) {
          query.category2id = category2id;
        }
        if (category3id) {
          query.category3id = category3id;
        }
        // 编程式导航的时候 将自定义属性的值作为query传过去
        this.$router.push({
          name: "search",
          query,
          params,
        });
      }
    },
    enter() {
      this.isShow = true;
    },
    leave() {
      this.isShow = this.$route.meta.isNav;
    },
  },
  computed: {
    ...mapState("home", ["categoryList"]),
  },
};
</script>

<style lang="less" scoped>
.navAnimation-enter,
.navAnimation-leave-to {
  opacity: 0;
  height: 0;
}
.navAnimation-enter-active,
.navAnimation-leave-to-active {
  transition: all 0.3s linear;
}
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }
          &.active {
            background-color: #e1251b;
            .item-list {
              display: block;
            }
          }
        }
      }
    }
  }
}
</style>